package com.tjetc.view;

import com.tjetc.domain.Admin;
import com.tjetc.domain.Category;
import com.tjetc.proxy.ClientProxy;
import com.tjetc.service.CategoryService;
import com.tjetc.vo.Result;

import java.util.List;
import java.util.Scanner;

public class CategoryView {
    private static Scanner sc=new Scanner(System.in);
    private static CategoryService categoryService= ClientProxy.getProxyInstance(CategoryService.class);
    public void start() {
        while (true){
            System.out.println("1.动物分类列表（如猫、狗、鸟类等）");
            System.out.println("2.添加动物分类");
            System.out.println("3.修改动物分类");
            System.out.println("4.删除动物分类");
            System.out.println("0.返回上一级");
            System.out.println("请输入你的选择0~4");
            int i = sc.nextInt();
            switch (i) {
                case 1:
                    list();
                    break;
                case 2:
                    add();
                    break;
                case 3:
                    update();
                    break;
                case 4:
                    delete();
                    break;
                case 0:
                    return;
                default:
                    System.out.println("请重新输入");
            }

        }
    }

    public void delete() {
        list();
        System.out.println("请输入将要删除的id");
        int id=sc.nextInt();
        Result result=categoryService.delete(id);
        System.out.println(result.getMsg());
    }

    public void update() {
        list();
        System.out.println("请输入id");
        int id = sc.nextInt();
        System.out.println("请输入名称");
        String name = sc.next();
        Result result=categoryService.update(new Category(id,name));
        System.out.println(result.getMsg());
    }

    public void add() {
        System.out.println("请输入名称");
        String name = sc.next();

        Result result=categoryService.add(new Category().setName(name));
        System.out.println(result.getMsg());
    }

    public void list() {
        Result result = categoryService.list();
        List<Category> list = (List<Category>) result.getData();
        if (list.size() > 0) {
            for (Category category : list) {
                System.out.println(category);
            }
        } else {
            System.out.println("没人啊");
        }

    }
}
